package edu.nanyang.struts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import edu.nanyang.domain.Goods;
import edu.nanyang.domain.Photo;
import edu.nanyang.domain.User;
import edu.nanyang.hibernate.Release;
import edu.nanyang.hibernate.service.IGoodsService;

public class ReleaseAction extends ActionSupport {
	@Autowired
	private IGoodsService goodsService;
	private Goods goods;
	private int schoolID;
	private File file;
	private String fileFileName;
	private String fileContentType;
	private static final int BUFFER_SIZE = 16 * 1024;
	
	@Override
	public String execute() {
		//发布信息的同时上传图片
		boolean s = true;
		System.out.println("fileContentType====="+fileContentType);
		try {
			Photo photo = savePhoto();
			User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
			s = goodsService.addGoods(goods,schoolID,photo,user);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (s) {
			return SUCCESS;
		} else
			return "input";
	}
	private Photo savePhoto() throws IOException{
		Photo photo=new Photo();
		String rootPath=(ServletActionContext.getServletContext().getRealPath("/upload").replace('\\', '/'))+"/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/";
		//创建目录
		File baseDir=new File(rootPath);
		if(baseDir.exists()){
			if(baseDir.isFile()){
				baseDir.delete();
				new File(rootPath).mkdirs();
			}else{
				//路径已经存在
			}
		}else{
			baseDir.mkdirs();
		}
		if(file==null){return photo;}
		InputStream in=new FileInputStream(file);
		String fileNameBefore=new Date().getTime()+"before."+fileFileName.substring(fileFileName.indexOf('.')+1);
		String fileNameAfter=new Date().getTime()+"after."+fileFileName.substring(fileFileName.indexOf('.')+1);
		File inFile1=new File(rootPath+fileNameBefore);
		File inFile2=new File(rootPath+fileNameAfter);
		System.out.println("rootPath+fileNameBefore========"+rootPath+fileNameBefore);
		System.out.println("rootPath+fileNameAfter========="+rootPath+fileNameAfter);
		boolean b1=inFile1.createNewFile();
		boolean b2=inFile2.createNewFile();
		if(!b1 || !b2){
			throw new IOException();
		}
		OutputStream out1=new FileOutputStream(inFile1);
		OutputStream out2=new FileOutputStream(inFile2);
		byte[] buffer = new byte[BUFFER_SIZE];      
		int length=0;
		int index=0;  
		while ((length = in.read(buffer)) > 0) {      
			out1.write(buffer, 0, length);
			//这里暂时先放一样的
			out2.write(buffer, 0, length);
			index++;
			if((index%100)==0){
				out1.flush();
				out2.flush();
			}
		}
		if(out1!=null){out1.close();}
		if(out2!=null){out2.close();}
		if(in!=null){in.close();}
		photo.setPathBefore("upload/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/"+fileNameBefore);
		photo.setPathAfter("upload/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/"+fileNameAfter);
		return photo;
	}
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public int getSchoolID() {
		return schoolID;
	}

	public void setSchoolID(int schoolID) {
		this.schoolID = schoolID;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	Release release;

	public Release getRelease() {
		return release;
	}

	public void setRelease(Release release) {
		this.release = release;
	}

	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	
}
